﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using DevExpress.Utils.Menu;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraBars;
using BSC_OTC_Management.BPS_Services;

namespace BSC_OTC_Management.Forms
{
    public partial class Frm_CancelOrder : Form
    {
        public Frm_CancelOrder()
        {
            InitializeComponent();
        }

        #region "KHAI BAO THAM SO CHUNG"

        Code.CL_Ultility _cl = new Code.CL_Ultility();
        public DataTable dtb_LenhHuy;
        int v_rCount;
        public DataTable Get_LenhHuy
        {
            get { return dtb_LenhHuy; }
            set { dtb_LenhHuy = value; }
        }

        #endregion
       
        private void Frm_CancelOrder_Load(object sender, EventArgs e)
        {
            try
            {
                gridControl3_Huy_lenh.DataSource = Get_LenhHuy;
                gridView5.BestFitColumns();
                v_rCount = Get_LenhHuy.Rows.Count;
                gridView5.SelectRows(0, v_rCount - 1);
                //gridView5.OptionsSelection.ResetSelectionClickOutsideCheckboxSelector = true;
            }
            catch
            {
            }
        }

        public string FAO_CANCELORDER(string pv_afacctno, string p_OrderID, string pv_tlid, int v_solenh)
        {
            try
            {
                const string functionName = "PLACEORDER";
                const string moduleName = "Orders";

                string[] keyField =
                {
                    "p_functionname", "p_username", "p_acctno", "p_afacctno", "p_exectype", "p_symbol", "p_quantity",
                    "p_quoteprice", "p_pricetype", "p_timetype", "p_book", "p_via", "p_dealid", "p_direct",
                    "p_effdate", "p_expdate", "p_tlid", "p_quoteqtty", "p_limitprice", "p_err_code", "p_err_message"
                };

                string[] keyValue =
                 {
                 "CANCELORDER", "", p_OrderID, pv_afacctno, "", "", "", "", "", "", "A", "H", "",
                 "Y", "", "", pv_tlid, "0", "0", "", ""
                 };

                string[] keyType =
                {
                    "VARCHAR2", "VARCHAR2", "VARCHAR2", "VARCHAR2", "VARCHAR2", "VARCHAR2", "NUMBER",
                    "NUMBER", "VARCHAR2", "VARCHAR2", "VARCHAR2", "VARCHAR2", "VARCHAR2", "VARCHAR2", "VARCHAR2", "VARCHAR2",
                    "VARCHAR2", "VARCHAR2", "VARCHAR2", "VARCHAR2", "VARCHAR2",
                };

                string[] keyDirection =
                {
                    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
                    "1", "1", "2", "2"
                };

                var bps = new OnlineTradingWcfClient();
                var result = bps.ExecBOFunction(functionName, moduleName, keyField, keyValue, keyType, keyDirection);
                string v_MaLoi = "";
                string V_DienGiai = "";
                foreach (var item in result)
                {
                    if (item.Key.Contains("p_err_code"))
                    {
                        v_MaLoi = item.Value;
                        V_DienGiai = _cl.LAY_THONG_TIN_LOI_TU_MA_LOI(v_MaLoi);
                    }
                }

                this.listBox_Ket_qua.Items.Add(v_solenh.ToString() + ".  " + p_OrderID + " - " + pv_afacctno + "   : " + V_DienGiai);
                return "";
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        private void btn_XacNhan_Click(object sender, EventArgs e)
        {
            this.btn_XacNhan.Enabled = false;
            try
            {
                string pv_afacctno, pv_Orderid, pv_tlid;
                int l = dtb_LenhHuy.Rows.Count;

                int v_solenh = 0;
                for (int i = 0; i < l; i++)
                {
                    pv_afacctno = dtb_LenhHuy.Rows[i]["CUSTID"].ToString();
                    pv_Orderid = dtb_LenhHuy.Rows[i]["orgorderid"].ToString();
                    v_solenh += 1;
                    pv_tlid = "6868";
                    FAO_CANCELORDER(pv_afacctno, pv_Orderid, pv_tlid, v_solenh);
                    _cl.Get_PlaceOrder_Status = true;
                }
                
                DialogResult rs= MessageBox.Show("Yêu cầu hủy lệnh đã được gửi vào hệ thống, Chọn màn hình trạng thái để xem kết quả hủy lệnh", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);

                if (rs == DialogResult.Yes)
                {
                    tabControl1.SelectedIndex = 1;
                }
                else
                {
                    this.Close();
                }
            }
            catch (Exception ex)
            { }
        }

        private void gridView5_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            try
            {
                gridView5.SelectRows(0, v_rCount - 1);
            }
            catch
            {
            }
        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}